﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
    /// <summary>
    /// 图片选择工具控件
    /// </summary>
    public partial class SARImageSelector : UserControl
    {
        public SARImageSelector()
        {
            this.InitializeComponent();
        }


        private ImageSelectionMode imgSelectionMode = ImageSelectionMode.RetangleMode;

        /// <summary>
        /// 控件工作模式，分为选点模式、选矩形模式和拖拽模式
        /// </summary>
        public ImageSelectionMode SARImgSelectionMode
        {
            get { return imgSelectionMode; }
            set { 
                imgSelectionMode = value;
                if (value == ImageSelectionMode.DragMode)
                {
                    scrollImg.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    scrollImg.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                }
                else
                {
                    scrollImg.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
                    scrollImg.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
                }
            }
        }

        /// <summary>
        /// 图像标题，如果设置为“”的话，则隐藏
        /// </summary>
        public string SARTitle
        {
            set
            {
                txtTitle.Text = value;
                if (value == "")
                {
                    bderTitle.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    bderTitle.Visibility = System.Windows.Visibility.Visible;
                }
            }
            get
            {
                return txtTitle.Text;
            }
        }

        /// <summary>
        /// 设置或者获取图片的内容
        /// </summary>
        public ImageSource SARImageSource
        {
            set
            {
                this.img.Source = value;
                this.gridSelected.Visibility = System.Windows.Visibility.Hidden;
            }
            get
            {
                return this.img.Source;
            }
        }

        private Point startPoint;
        private Point endPoint;

        private void imgContainer_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.imgSelectionMode == ImageSelectionMode.RetangleMode)
            {
                double selectionWidth;
                double selectionHeight;
                if (img.ActualWidth < imgPanel.ActualWidth)
                {
                    selectionHeight = img.ActualHeight / 6;
                    selectionWidth = imgPanel.ActualWidth / imgPanel.ActualHeight * selectionHeight;
                }
                else
                {
                    selectionWidth = img.ActualWidth / 6;
                    selectionHeight = imgPanel.ActualHeight / imgPanel.ActualWidth * selectionWidth;
                }
                Point mouseposition = e.GetPosition(imgContainer);
                if (mouseposition.X < (selectionWidth / 2))
                {
                    mouseposition.X = selectionWidth / 2;
                }
                if (mouseposition.X > (img.ActualWidth - (selectionWidth / 2)))
                {
                    mouseposition.X = img.ActualWidth - (selectionWidth / 2);
                }

                if (mouseposition.Y < (selectionHeight / 2))
                {
                    mouseposition.Y = selectionHeight / 2;
                }
                if (mouseposition.Y > (img.ActualHeight - (selectionHeight / 2)))
                {
                    mouseposition.Y = img.ActualHeight - (selectionHeight / 2);
                }

                gridSelection.Width = selectionWidth;
                gridSelection.Height = selectionHeight;
                gridSelection.Margin = new Thickness(mouseposition.X - selectionWidth / 2, mouseposition.Y - selectionHeight / 2, 0, 0);
                gridSelection.Visibility = System.Windows.Visibility.Visible;
            }
            else if(this.imgSelectionMode == ImageSelectionMode.PointMode)
            {
                gridSelection.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (this.imgSelectionMode == ImageSelectionMode.DragMode)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    endPoint = e.GetPosition(imgContainer);
                    ShowSelectionRetangle(startPoint, endPoint);
                }
            }
        }

        private void imgContainer_MouseLeave(object sender, MouseEventArgs e)
        {            
            gridSelection.Visibility = System.Windows.Visibility.Hidden;
        }
                
        /// <summary>
        /// 用户选择了图像区域的事件,imgAreaWidth,imgAreaHeight代表图像的显示区域的大小
        /// selectionLeftTop，selectionRightBottom代表了选择区域的左上角和右下角的坐标
        /// x轴的坐标是从左到右，y轴坐标是从上到下
        /// </summary>
        public event ImageSelectionHandler SARImageSelect;
        private void gridSelection_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.SARImgSelectionMode == ImageSelectionMode.RetangleMode)
            {
                double imgAreaWidth = img.ActualWidth;
                double imgAreaHeight = img.ActualHeight;
                Point selectionLeftTop = new Point(gridSelection.Margin.Left, gridSelection.Margin.Top);
                Point selectionRightBottom = new Point(selectionLeftTop.X + gridSelection.ActualWidth, selectionLeftTop.Y + gridSelection.Height);

                gridSelected.Width = gridSelection.Width;
                gridSelected.Height = gridSelection.Height;
                gridSelected.Margin = new Thickness(selectionLeftTop.X, selectionLeftTop.Y, 0, 0);
                this.gridSelected.Visibility = System.Windows.Visibility.Visible;

                if (SARImageSelect != null && selectionLeftTop.X != selectionRightBottom.X && selectionLeftTop.Y != selectionRightBottom.Y)
                {
                    SARImageSelect.Invoke(imgAreaWidth, imgAreaHeight, selectionLeftTop, selectionRightBottom);
                }
            }
        }

        /// <summary>
        /// 在点模式下，用户选择了一个点
        /// imgAreaWidth,imgAreaHeight代表图像的显示区域的大小
        /// selectionPoint表示用户选择的某个点
        /// x轴的坐标是从左到右，y轴坐标是从上到下
        /// </summary>
        public event ImagePointSelectionHandler SARImagePointSelect;

        private void imgContainer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.SARImgSelectionMode == ImageSelectionMode.PointMode)
            {
                double imgAreaWidth = img.ActualWidth;
                double imgAreaHeight = img.ActualHeight;
                if (SARImagePointSelect != null)
                {
                    SARImagePointSelect.Invoke(imgAreaWidth, imgAreaHeight, e.GetPosition(imgContainer));
                }
            }
            else if(this.SARImgSelectionMode== ImageSelectionMode.DragMode)
            {
                startPoint = e.GetPosition(imgContainer);
            }
        }

        private void imgContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.imgSelectionMode == ImageSelectionMode.DragMode)
            {
                double imgAreaWidth = img.ActualWidth;
                double imgAreaHeight = img.ActualHeight;
                Point selectionLeftTop = new Point(gridSelection.Margin.Left, gridSelection.Margin.Top);
                Point selectionRightBottom = new Point(selectionLeftTop.X + gridSelection.ActualWidth, selectionLeftTop.Y + gridSelection.Height);

                gridSelected.Width = gridSelection.Width;
                gridSelected.Height = gridSelection.Height;
                gridSelected.Margin = new Thickness(selectionLeftTop.X, selectionLeftTop.Y, 0, 0);
                this.gridSelected.Visibility = System.Windows.Visibility.Visible;
                this.gridSelection.Visibility = System.Windows.Visibility.Hidden;

                if (SARImageSelect != null && selectionLeftTop.X != selectionRightBottom.X && selectionLeftTop.Y != selectionRightBottom.Y)
                {
                    SARImageSelect.Invoke(imgAreaWidth, imgAreaHeight, selectionLeftTop, selectionRightBottom);
                }
            }
        }

        private void ShowSelectionRetangle(Point startPoint, Point endPoint)
        {
            double lefttopx = startPoint.X < endPoint.X ? startPoint.X : endPoint.X;
            double lefttopy = startPoint.Y < endPoint.Y ? startPoint.Y : endPoint.Y;
            double rightbottomx = startPoint.X > endPoint.X ? startPoint.X : endPoint.X;
            double rightbottomy = startPoint.Y > endPoint.Y ? startPoint.Y : endPoint.Y;

            gridSelection.Margin = new Thickness(lefttopx, lefttopy, 0, 0);
            gridSelection.Width = rightbottomx - lefttopx;
            gridSelection.Height = rightbottomy - lefttopy;

            gridSelection.Visibility = System.Windows.Visibility.Visible;
        }

        private void imgContainer_MouseEnter(object sender, MouseEventArgs e)
        {
            if (this.imgSelectionMode == ImageSelectionMode.DragMode)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    startPoint = e.GetPosition(imgContainer);
                }
            }
        }
    }

    /// <summary>
    /// 图像选择控件的工作模式
    /// </summary>
    public enum ImageSelectionMode
    {
        RetangleMode,
        PointMode,
        DragMode
    }

    /// <summary>
    /// 图像区域被选择后的处理函数
    /// </summary>
    /// <param name="imgAreaWidth">图像宽度</param>
    /// <param name="imgAreaHeight">图像高度</param>
    /// <param name="selectionLeftTop">被选择区域左上角坐标</param>
    /// <param name="selectionRightBottom">被选择区域右下角坐标</param>
    public delegate void ImageSelectionHandler(double imgAreaWidth, double imgAreaHeight, Point selectionLeftTop, Point selectionRightBottom);

    /// <summary>
    /// 图像中的点被选择后的处理函数
    /// </summary>
    /// <param name="imgAreaWidth">图像宽度</param>
    /// <param name="imgAreaHeight">图像高度</param>
    /// <param name="selectionPoint">被选择点的坐标</param>
    public delegate void ImagePointSelectionHandler(double imgAreaWidth, double imgAreaHeight, Point selectionPoint);
}